package com.example.testproject.activity

import android.content.ComponentName
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.testproject.R
import com.example.testproject.ext.bottomDialog
import com.google.android.material.bottomappbar.BottomAppBar
import kotlinx.android.synthetic.main.activity_md.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.callbackFlow


/**
 *
 * @ClassName: MdActivity
 * @Author: android 超级兵
 * @CreateDate: 11/11/21$ 3:56 PM$
 * TODO md风格测试
 */
class MdActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_md)


        floatingButton.setOnClickListener {
            if (bottomAppBar.fabAlignmentMode == BottomAppBar.FAB_ALIGNMENT_MODE_END) {
                bottomAppBar.fabAlignmentMode = BottomAppBar.FAB_ALIGNMENT_MODE_CENTER
            } else {
                bottomAppBar.fabAlignmentMode = BottomAppBar.FAB_ALIGNMENT_MODE_END
            }

            bottomDialog(View.inflate(this, R.layout.dialog_goods_sku_layout, null)).apply {
                show()
            }

            // openOther()
        }
    }

    /*
     * 作者:android 超级兵
     * 创建时间: 11/25/21 10:37 AM
     * TODO 打开其他APP
     */
    private fun openOther() {
        // 打开app
        val packName = "com.**.**.wanandroid"
        val packageManager = packageManager
        if (checkPackInfo(packName)) {
            val intent = packageManager.getLaunchIntentForPackage(packName)
            startActivity(intent)
        } else {
            Toast.makeText(this, "没有安装${packName}", Toast.LENGTH_SHORT).show()
        }

        // 打开app指定页面 需要将目标也Activity设置为:android:exported="true"
        val intent = Intent()
        //第一种方式
        val cn = ComponentName(packName, "com.****.SettingActivity")
        try {
            intent.component = cn
            //第二种方式
            //intent.setClassName("com.example.fm", "com.example.fm.MainFragmentActivity");
            startActivity(intent)
        } catch (e: Exception) {
            //TODO  可以在这里提示用户没有安装应用或找不到指定Activity，或者是做其他的操作
        }
    }

    /**
     * 检查包是否存在
     *
     * @param packName
     * @return
     */
    private fun checkPackInfo(packName: String): Boolean {
        var packageInfo: PackageInfo? = null
        try {
            packageInfo = packageManager.getPackageInfo(packName, 0);
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return packageInfo != null
    }
}